package com.seuic.scannerapitest.activity;

import android.util.Log;

import com.seuic.misc.Misc;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DeviceModel {
    private static final String TAG = "Device";

    public enum DeviceType {
        device_unknown,
        device_d500p,
        device_d510p,
        device_d330s,
        device_d700,
        device_d700c,
    }

    /**
     * PDA Model
     */
    public static final String MODEL_D510P_PRI = "AUTOIDPAD";
    public static final String MODEL_D510P = "PDT-APAD";

    public static final String MODEL_D500P_PRI = "AUTOID9";
    public static final String MODEL_D500P = "PDT-90P";

    public static final String MODEL_D330S_PRI = "AUTOID6L";
    public static final String MODEL_D330S = "PDT-6LP";

    public static final String MODEL_CRUISE = "CRUISE";

    public static final String MODEL_CRUISE_C = "CRUISE-C";

    /**
     * Get PDA model
     *
     * @return PDA model
     */
    public static String getPriModel() {
        return SystemProperties.get("ro.product.primodel", "");
    }

    public static String getModel() {
        return SystemProperties.get("ro.product.model", "");
    }

    public static String getInternalModel() {
        return SystemProperties.get("ro.product.internal", "");
    }

    public static DeviceType getDeviceType() {
        String priModel = getPriModel();
        String model = getModel();
        String internalModel = getInternalModel();

        if (MODEL_D510P_PRI.equals(priModel) || MODEL_D510P.equals(model))
            return DeviceType.device_d510p;
        else if (MODEL_D500P_PRI.equals(priModel) || MODEL_D500P.equals(model))
            return DeviceType.device_d500p;
        else if (MODEL_D330S_PRI.equals(priModel) || MODEL_D330S.equals(model))
            return DeviceType.device_d330s;
        else if (MODEL_CRUISE.equals(internalModel))
            return DeviceType.device_d700;
        else if (MODEL_CRUISE_C.equals(internalModel))
            return DeviceType.device_d700c;

        return DeviceType.device_unknown;
    }

    public static String getDeviceId() {
        Misc misc = new Misc();
        return misc.getSN();
    }

    public static String getCustomId() {
        return SystemProperties.get("gsm.seuic.customid", "");
    }

    public static String getBaseband() {
        return SystemProperties.get("gsm.version.baseband", "");
    }

    public static String getScannerType() {
        return readFile("/sys/class/padinfo/scanner");
    }

    public static String getCameraType() {
        return readFile("/sys/class/padinfo/camera");
    }

    public static String getDevFunc() {
        return readFile("/sys/class/padinfo/device_type");
    }

    public static String getKernelVersion() {
        String rawKernelVersion = readFile("/proc/version");

        final String PROC_VERSION_REGEX =
                "Linux version (\\S+) " + /* group 1: "3.0.31-g6fb96c9" */
                        "\\((\\S+?)\\) " +        /* group 2: "x@y.com" (kernel builder) */
                        "(?:\\(gcc.+? \\)) " +    /* ignore: GCC version information */
                        "(#\\d+) " +              /* group 3: "#1" */
                        "(?:.*?)?" +              /* ignore: optional SMP, PREEMPT, and any CONFIG_FLAGS */
                        "((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)"; /* group 4: "Thu Jun 28 11:02:39 PDT 2012" */

        //String rawKernelVersion = readLine("/proc/version");
        Matcher m = Pattern.compile(PROC_VERSION_REGEX).matcher(rawKernelVersion);
        if (!m.matches()) {
            Log.e(TAG, "Regex did not match on /proc/version: " + rawKernelVersion);
            return "Unavailable";
        } else if (m.groupCount() < 4) {
            Log.e(TAG, "Regex match on /proc/version only returned " + m.groupCount()
                    + " groups");
            return "Unavailable";
        }
        return m.group(1) + "\n" +                 // 3.0.31-g6fb96c9
                m.group(2) + " " + m.group(3) + "\n" + // x@y.com #1
                m.group(4);                            // Thu Jun 28 11:02:39 PDT 2012
    }

    public static String getSN() {
        String SN = "";
        try {
//			Misc misc = new Misc();
//			SN = misc.getSN();
        } catch (Exception e) {

        }
        return SN;
    }

    public static String getCali() {
        String Cali = "";
        try {
//			Misc misc = new Misc();
//			Cali = misc.getCali();
        } catch (Exception e) {

        }
        return Cali;
    }

    public static String readFile(String filename) {
        try {
            return readLine(filename);
        } catch (IOException e) {
            Log.e(TAG, filename + ": IO Exception ", e);
            return "";
        }
    }

    /**
     * Reads a line from the specified file.
     *
     * @param filename the file to read from
     * @return the first line, if any.
     * @throws IOException if the file couldn't be read
     */
    private static String readLine(String filename) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(filename), 256);
        try {
            return reader.readLine();
        } finally {
            reader.close();
        }
    }
}
